package com.stylefeng.guns.modular.system.api;


import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.core.util.DateUtils;
import com.stylefeng.guns.core.util.IpUtil;
import com.stylefeng.guns.core.util.NetworkUtil;
import com.stylefeng.guns.modular.system.model.UserinfoActive;
import com.stylefeng.guns.modular.system.service.IUserinfoActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/api")
public class UserinfoActiveApiController extends BaseController {

    @Autowired
    IUserinfoActiveService userinfoActiveService;

    @RequestMapping(value = "/phoneActive")
    @Transactional
    public JsonResult addPhoneUserinfo(HttpServletRequest request){
        String location = "";
        String ip = null;
        String dizhiArr [] = null;
        UserinfoActive userinfo = null;
        try {
            ip = NetworkUtil.getIpAddress(request);

            userinfo = getPhoneUserInfo(request);

            if("127.0.0.1".equals(ip)) {
                userinfo.setPhoneCountry("局域网");
                userinfo.setPhoneCity("局域网");
                userinfo.setPhoneProvince("局域网");
                userinfo.setReqip(ip);
            }else{
                dizhiArr = IpUtil.getAddress(ip,location);
                if(dizhiArr!=null) {
                    userinfo.setPhoneCountry(dizhiArr[0]);
                    userinfo.setPhoneCity(dizhiArr[3]);
                    userinfo.setPhoneProvince(dizhiArr[2]);
                    userinfo.setReqip(ip);
                }else{
                    userinfo.setReqip(ip);
                    userinfo.setPhoneCountry("局域网");
                    userinfo.setPhoneCity("局域网");
                    userinfo.setPhoneProvince("局域网");
                }
            }

            userinfoActiveService.insert(userinfo,DateUtils.getDate(new Date(),"yyyy_MM_dd"));

            return ResultGenerator.genSuccessResult();

        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult(e.getMessage());
        }finally {
            dizhiArr = null;
            userinfo = null;
            ip = null;
        }
    }

    /**
     * 获取用户信息
     * */
    public UserinfoActive getPhoneUserInfo(HttpServletRequest request){
        UserinfoActive phoneUserinfo = new UserinfoActive();
        try {
            phoneUserinfo.setOs(request.getParameter("os"));
            phoneUserinfo.setPhoneResolution(request.getParameter("phoneResolution"));
            phoneUserinfo.setPhoneUdid(request.getParameter("phoneUdid"));
            phoneUserinfo.setAppkey(request.getParameter("appkey"));
            phoneUserinfo.setSdkVersion(request.getParameter("sdkVersion"));
            phoneUserinfo.setCurrentnetworktype(request.getParameter("currentnetworktype"));
            phoneUserinfo.setCpid(request.getParameter("cpid"));
            phoneUserinfo.setPhoneSoftname(request.getParameter("phoneSoftname"));
            phoneUserinfo.setPhoneSoftversion(request.getParameter("phoneSoftversion"));
            phoneUserinfo.setType(request.getParameter("type"));
            phoneUserinfo.setPhoneImsi(request.getParameter("phoneImsi"));
            phoneUserinfo.setPhoneImei(request.getParameter("phoneImei"));
            phoneUserinfo.setPhoneMac(request.getParameter("phoneMac"));
            phoneUserinfo.setPhoneApn(request.getParameter("phoneApn"));
            phoneUserinfo.setPhoneFirmwareVersion(request.getParameter("phoneFirmwareVersion"));
            phoneUserinfo.setPhoneBrand(request.getParameter("phoneBrand"));
            phoneUserinfo.setPhoneModel(request.getParameter("phoneModel"));
            phoneUserinfo.setAddtime(DateUtils.getDateTime());
            phoneUserinfo.setSid(request.getParameter("sid"));
            phoneUserinfo.setCoreSdkVersion(request.getParameter("coreSdkVersion"));
            phoneUserinfo.setCodeLocation(request.getParameter("codeLocation"));
            phoneUserinfo.setBehaviorOp(request.getParameter("behaviorOp"));
            phoneUserinfo.setAndroidid(request.getParameter("androidid"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return phoneUserinfo;
    }


}
